package com.piggy.im.server.mq;

import com.piggy.common.core.constant.SecurityConstants;
import com.piggy.im.domain.bo.SmsMessage;
import com.piggy.im.mq.model.SmsMsgMQ;
import com.piggy.im.server.service.ISysStationMessagePublishService;
import com.piggy.mq.executor.MqThreadExecutor;
import com.piggy.mq.vender.IMQMsgReceiver;
import com.piggy.sys.api.RemoteSmsService;
import com.piggy.sys.api.bo.SmsNotify;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
public class SmsMQReceiver implements IMQMsgReceiver {

    @Resource
    private RemoteSmsService smsService;

    @Override
    @Async(MqThreadExecutor.EXECUTOR_NOTIFY)
    @RabbitListener(queues = SmsMsgMQ.MQ_NAME)
    public void receiveMsg(String msg) {
        SmsMessage bo = SmsMsgMQ.parse(msg);
        try {
            bo.getPhoneNumbers().forEach(t-> smsService.smsNotify(t, new SmsNotify().setParams(bo.getParamMap()).setTemplateCode(bo.getTemplateCode()), SecurityConstants.INNER));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

}
